/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.etrs.formathlon.entity.formation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import net.etrs.formathlon.entity.utilisateur.Personnel;
import net.etrs.formathlon.entity.utils.ComparableEvenThoughUnpersisted;
import net.etrs.formathlon.persistance.utils.JpaRequestNames;

/**
 * Entité symbolisant le théorique d'une formation.
 * @author OPIF
 */
@NamedQueries({
    @NamedQuery(name = JpaRequestNames.LISTE_FORMATION_EN_COURS_DOMAINE,
    query = "select object(f) from Formation f where f.archive = false and f.domaine = :domaine "
    + "and (f.dateFin is null or f.dateFin > :date)"),
    @NamedQuery(name = JpaRequestNames.LISTE_FORMATION_COMPLEMENTAIRE,
    query = "select object(f) from Formation f where f.archive = false and f.domaine = :domaine "
    + "and f.complementaire = :complementaire"),
    @NamedQuery(name = JpaRequestNames.LISTE_FORMATION_EN_COURS,
    query = "select object(f) from Formation f where f.archive = false "
    + "and (f.dateFin is null or f.dateFin > :date)")
})
@Entity
public class Formation extends ComparableEvenThoughUnpersisted<Formation> {

    private static final long serialVersionUID = 1L;
    
    /**
     * Identifiant
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    /**
     * Libellé de la formation.
     */
    private String libelle;
    
    /**
     * Description de la formation.
     */
    private String description;
    
    /**
     * Si vrai c'est une formation complémentaire sinon de base.
     */
    private boolean complementaire;
    
    /**
     * Date ou à comencé à être dispensé la formation.
     */
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateDebut;
    
    /**
     * Date à partir duquel on ne dispensera plus de formation.
     */
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateFin;

    /**
     * Liste des séances théoriques composant la formation.
     */
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "formation")
    private List<SeanceTh> listSeanceTh = new ArrayList<SeanceTh>();
    
    /**
     * Liste des instructeurs pouvant dispenser cette formation.
     */
    @ManyToMany
    @JoinTable(name = "specialites",
    joinColumns =
    @JoinColumn(name = "formation_id", referencedColumnName = "id"),
    inverseJoinColumns =
    @JoinColumn(name = "personnel_id", referencedColumnName = "id"))
    private List<Personnel> listInstructeur = new ArrayList<Personnel>();
    
    /**
     * Liste des sessions qui dispense cette formation.
     */
    @OneToMany(mappedBy = "formation", cascade = CascadeType.ALL)
    private List<Session> listSession = new ArrayList<Session>();
    
    /**
     * Domaine général dans lequel s'incrit cette formation.
     */
    @ManyToOne
    private Domaine domaine;
    
    /**
     * Liste des participations, qui ont comme attribut la personne qui a participée
     * à une session de cette formation.
     */
    @OneToMany(mappedBy = "formation")
    private List<Participation> listParticipation;
    private boolean archive;

    /**
     * 
     * @return Id
     */
    @Override
    public Long getId() {
        return id;
    }

    /**
     * Setter de l'id.
     * @param id 
     */
    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return String.format("Formation id=%d, libelle=%s", id, getLibelle());
    }

    /**
     * @return the libelle
     */
    public String getLibelle() {
        return libelle;
    }

    /**
     * @param libelle the libelle to set
     */
    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the dateDebut
     */
    public Date getDateDebut() {
        return dateDebut;
    }

    /**
     * @param dateDebut the dateDebut to set
     */
    public void setDateDebut(Date dateDebut) {
        this.dateDebut = dateDebut;
    }

    /**
     * @return the dateFin
     */
    public Date getDateFin() {
        return dateFin;
    }

    /**
     * @param dateFin the dateFin to set
     */
    public void setDateFin(Date dateFin) {
        this.dateFin = dateFin;
    }

    /**
     * @return the listSeanceTh
     */
    public List<SeanceTh> getListSeanceTh() {
        return listSeanceTh;
    }

    /**
     * @param listSeanceTh the listSeanceTh to set
     */
    public void setListSeanceTh(List<SeanceTh> listSeanceTh) {
        this.listSeanceTh = listSeanceTh;
    }

    /**
     * @return the listInstructeur
     */
    public List<Personnel> getListInstructeur() {
        return listInstructeur;
    }

    /**
     * @param listInstructeur the listInstructeur to set
     */
    public void setListInstructeur(List<Personnel> listInstructeur) {
        this.listInstructeur = listInstructeur;
    }

    /**
     * @return the listSession
     */
    public List<Session> getListSession() {
        return listSession;
    }

    /**
     * @param listSession the listSession to set
     */
    public void setListSession(List<Session> listSession) {
        this.listSession = listSession;
    }

    /**
     * @return the domaine
     */
    public Domaine getDomaine() {
        return domaine;
    }

    /**
     * @param domaine the domaine to set
     */
    public void setDomaine(Domaine domaine) {
        this.domaine = domaine;
    }

    /**
     * @return the complementaire
     */
    public boolean isComplementaire() {
        return complementaire;
    }

    /**
     * @param complementaire the complementaire to set
     */
    public void setComplementaire(boolean complementaire) {
        this.complementaire = complementaire;
    }

    @Override
    public boolean isArchive() {
        return archive;
    }

    @Override
    public void setArchive(boolean archive) {
        this.archive = archive;
    }

    /**
     * @return the listParticipation
     */
    public List<Participation> getListParticipation() {
        return listParticipation;
    }

    /**
     * @param listParticipation the listParticipation to set
     */
    public void setListParticipation(List<Participation> listParticipation) {
        this.listParticipation = listParticipation;
    }
}
